import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class GalleryPhotoViewWrapper extends StatefulWidget {
  GalleryPhotoViewWrapper({
    this.loadingBuilder,
    this.backgroundDecoration,
    this.minScale,
    this.maxScale,
    this.initialIndex,
    @required this.galleryItems,
    this.scrollDirection = Axis.horizontal,
    this.imageSource = 0,
    this.imageType
  }) : pageController = PageController(initialPage: initialIndex);

  final LoadingBuilder loadingBuilder;
  final Decoration backgroundDecoration;
  final dynamic minScale;
  final dynamic maxScale;
  final int initialIndex;
  final PageController pageController;
  //final List<File> galleryItems;
  final List<dynamic> galleryItems;
  final Axis scrollDirection;
  final int imageSource; // 0 本地文件， 1 网络  3.（网络，文件）混合
  final List<dynamic> imageType;//图片类型数组
  @override
  State<StatefulWidget> createState() {
    return _GalleryPhotoViewWrapperState(imageSource,imageType);
  }
}

class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
  int currentIndex;
  final int imageSource; // 0 本地文件， 1 网络
  final List<dynamic> imageType;//图片类型数组
  _GalleryPhotoViewWrapperState(this.imageSource,this.imageType);
  @override
  void initState() {
    currentIndex = widget.initialIndex;
    super.initState();
  }

  void onPageChanged(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 1024, height: 640);
    return Scaffold(
      body: Container(
        decoration: widget.backgroundDecoration,
        constraints: BoxConstraints.expand(
          height: MediaQuery.of(context).size.height,
        ),
        child: Stack(
          alignment: Alignment.bottomRight,
          children: <Widget>[
            PhotoViewGallery.builder(
              scrollPhysics: const BouncingScrollPhysics(),
              builder: _buildItem,
              itemCount: widget.galleryItems.length,
              loadingBuilder: widget.loadingBuilder,
              backgroundDecoration: widget.backgroundDecoration,
              pageController: widget.pageController,
              onPageChanged: onPageChanged,
              scrollDirection: widget.scrollDirection,
            ),
            Align(
              alignment: Alignment.topLeft,
              child: InkWell(
                onTap: (){
                  Navigator.pop(context);
                },
                child: Container(
                  padding: EdgeInsets.only(left: ScreenUtil().setWidth(20), top: ScreenUtil().setWidth(20)),
                  child: Image.asset("images/back.png", width: ScreenUtil().setWidth(30), height : ScreenUtil().setWidth(30)),
                )
              ),
            ),
            Container(
              padding: const EdgeInsets.all(20.0),
              child: Text(
                "Image ${currentIndex}",
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 17.0,
                  decoration: null,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
    //final File item = widget.galleryItems[index];
    final  item = widget.galleryItems[index];
    return  PhotoViewGalleryPageOptions(
      imageProvider: imageSource !=3?getFileImage(imageSource, item):getImageType(index,item),//FileImage(item),
      initialScale: PhotoViewComputedScale.contained,
      minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
      maxScale: PhotoViewComputedScale.covered * 1.1,
      heroAttributes: PhotoViewHeroAttributes(tag: index),
    );
  }
  ImageProvider getImageType(int index,final item){
    int type =imageType[index];
    switch(type){
      case 0:
        return FileImage(item);
        break;
      case 1:
        String url = item as String;
        return NetworkImage(url);
        break;
      default:
        break;
    }
  }
  ImageProvider getFileImage(int type, final item){
    switch(type){
      case 0:
        return FileImage(item);
        break;
      case 1:
        String url = item as String;
        return NetworkImage(url);
        break;
      default:
        break;
    }
  }
}

